package com.jinkaimen.kqb.ui.activity.addmerchant;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.UIUtils;
import com.jinkaimen.kqb.R;
import com.jinkaimen.kqb.bean.BindQrCodeBean;
import com.jinkaimen.kqb.bean.ReportShopInfoGetBean;
import com.jinkaimen.kqb.manager.ApiManager;
import com.jinkaimen.kqb.utils.URLUtils;

import retrofit2.Call;
import retrofit2.Response;


public class BindQrCodeActivity extends BaseActivity {

    private long mShopId;
    private TextView mShopNameView;
    private TextView mTelView;
    private TextView mBtnView;
    private long mUserId;
    private ReportShopInfoGetBean mReportShopInfoGetBean;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initTitleBar() {
        mTitleBarView.setTitleText("提交成功");
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_bind_qr_code;
    }

    @Override
    protected void initChildView() {
        mShopNameView = (TextView) findViewById(R.id.tv_bind_shop_name);
        mTelView = (TextView) findViewById(R.id.tv_bind_shop_tel);
        mBtnView = (TextView) findViewById(R.id.btn_activity_withdraw_success);
    }

    @Override
    protected void initChildData() {
        showLoadingDialog();
        ApiManager.getApi().reportGetShopInfo(
                "INV_HSY001010",
                mUserId
        ).enqueue(new NetCallBack<ReportShopInfoGetBean>() {

            @Override
            protected void onSuccess(Call<ReportShopInfoGetBean> call, Response<ReportShopInfoGetBean> response) {
                mReportShopInfoGetBean = response.body();
                mShopNameView.setText("商户全称："+ mReportShopInfoGetBean.appBizShop.name);
                mTelView.setText("登录名："+mReportShopInfoGetBean.appAuUser.cellphone);
                mBtnView.setText("对主店("+mReportShopInfoGetBean.appBizShop.shortName+")绑定机具");

            }

            @Override
            protected void hiddenProgress() {
                hideLodingDialog();
            }
        });
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mShopId = callIntent.getLongExtra("report_shop_id", 0);
        mUserId = callIntent.getLongExtra("report_account_id", 0);
    }

    @Override
    protected void setChildViewListener() {
        mBtnView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn_activity_withdraw_success:
                startScanQrcode();
                break;
        }
    }

    public void startScanQrcode(){
        //                假如你要用的是fragment进行界面的跳转
//                IntentIntegrator intentIntegrator = IntentIntegrator.forSupportFragment(ShopFragment.this)
//
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator
                .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
                .setPrompt("将二维码/条码放入框内，即可自动扫描")//写那句提示的话
                .setOrientationLocked(false)//扫描方向固定
                .setCaptureActivity(QrCodeActivity.class) // 设置自定义的activity是CustomActivity
                .initiateScan(); // 初始化扫描
    }


    //获取扫描的结果
    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (intentResult != null) {
            if (intentResult.getContents() != null) {
                // ScanResult 为获取到的字符串
                String url = intentResult.getContents();
                final String qrCode = URLUtils.getValueByName(url, "code");
                ApiManager.getApi().reportBindQrCode(
                        "INV_HSY001011",
                        mUserId,
                        qrCode,
                        mShopId
                ).enqueue(new NetCallBack<BindQrCodeBean>() {
                    @Override
                    protected void onSuccess(Call<BindQrCodeBean> call, Response<BindQrCodeBean> response) {
                        BindQrCodeBean bindQrCodeBean = response.body();
                        if (bindQrCodeBean != null) {
                            UIUtils.showToast("绑定二维码成功");

                            Bundle bundle = new Bundle();
                            bundle.putString("qr_code", qrCode);
                            bundle.putString("shop_name", mReportShopInfoGetBean.appBizShop.shortName);
                            bundle.putString("zhifubao_rate", bindQrCodeBean.appBizShop.alipayRate*100+"%");
                            bundle.putString("weixin_rate", bindQrCodeBean.appBizShop.weixinRate*100+"%");
                            ActivityUtils.startActivityAndFinish(BindQrCodeActivity.this, BindSuccessActivity.class, bundle);
                        }

                    }

                    @Override
                    protected boolean isNullNeedCallback() {
                        return true;
                    }
                });
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }
}
